package com.example.movie.controller;

import com.example.movie.entity.TUser;
import com.example.movie.service.TUserService;
import com.example.movie.vo.RestResult;
import com.example.movie.vo.RestResultBuilder;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.List;


/**
 * (TUser)控制层
 *
 * @author makejava
 * @since 2021-04-02 16:51:14
 */
@RestController
@RequestMapping("/tUser")
@CrossOrigin
public class TUserController {
    /**
     * 服务对象
     */
    @Resource
    private TUserService tUserService;

    /**
     * 通过主键查询单条数据
     *
     * @param tUser 参数对象
     * @return 单条数据
     */
    @RequestMapping(value = "get", method = RequestMethod.GET)
    public RestResult selectOne(TUser tUser) {
        TUser result = tUserService.selectById(tUser.getUserId());
        if (result != null) {
            return new RestResultBuilder<>().success("查询成功", result);
        } else {
            return new RestResultBuilder<>().error("查询失败");
        }
    }

    /**
     * 新增一条数据
     *
     * @param tUser 实体类
     * @return Response对象
     */
    @RequestMapping(value = "insert", method = RequestMethod.POST)
    public RestResult insert(@RequestBody TUser tUser) {
        int result = tUserService.insert(tUser);
        if (result > 0) {
            return new RestResultBuilder<>().success("新增成功", tUser);
        }
        return new RestResultBuilder<>().error("新增失败");
    }

    /**
     * 修改一条数据
     *
     * @param tUser 实体类
     * @return Response对象
     */
    @RequestMapping(value = "update", method = RequestMethod.POST)
    public RestResult update(@RequestBody TUser tUser) {
        TUser result = tUserService.update(tUser);
        if (result != null) {
            return new RestResultBuilder<>().success("修改成功", result);
        }
        return new RestResultBuilder<>().error("修改失败");
    }

    /**
     * 删除一条数据
     *
     * @param tUser 参数对象
     * @return Response对象
     */
    @RequestMapping(value = "delete", method = RequestMethod.POST)
    public RestResult delete(@RequestBody TUser tUser) {
        int result = tUserService.deleteById(tUser.getUserId());
        if (result > 0) {
            return new RestResultBuilder<>().success("删除成功", null);
        }
        return new RestResultBuilder<>().error("删除失败");
    }

    /**
     * 查询全部
     *
     * @return Response对象
     */
    @RequestMapping(value = "selectAll", method = RequestMethod.GET)
    public RestResult selectAll() {
        List<TUser> tUsers = tUserService.selectAll();
        if (tUsers != null) {
            return new RestResultBuilder<>().success("查询成功", tUsers);
        }
        return new RestResultBuilder<>().error("查询失败");
    }

    /**
     * 分页查询
     *
     * @param start 偏移
     * @param limit 条数
     * @return Response对象
     */
    @RequestMapping(value = "selectPage", method = RequestMethod.GET)
    public RestResult selectPage(Integer start, Integer limit) {
        List<TUser> tUsers = tUserService.selectPage(start, limit);
        if (tUsers != null) {
            return new RestResultBuilder<>().success("查询成功", tUsers);
        }
        return new RestResultBuilder<>().error("查询失败");
    }

    @RequestMapping(value = "login", method = RequestMethod.POST)
    public RestResult login( TUser tUser) {
        List<TUser> tUsers = tUserService.selectList(tUser);
        if(tUsers.size()!=0){
            return new RestResultBuilder<>().success("登录成功",tUsers.get(0));
        }else {
            return new RestResultBuilder<>().error("登录失败");
        }
    }


    @RequestMapping(value = "register", method = RequestMethod.POST)
    public RestResult register( TUser tUser) {
        //获取邮箱
        String email = tUser.getEmail();
        //根据邮箱去数据库查询是否存在用户
        TUser user = tUserService.getUserByEmail(email);
        if(user != null){
            return new RestResultBuilder<>().success("该邮箱已被注册");
        }else{
            int result = tUserService.insert(tUser);
            if(result > 0){
                return new RestResultBuilder<>().success("注册成功");
            }else {
                return new RestResultBuilder<>().error("注册失败");
            }
        }
    }

    /**
     * 更新用户信息
     * @param tUser
     * @return
     */
    @RequestMapping(value = "updateUser", method = RequestMethod.POST)
    public RestResult updateUser(TUser tUser) {
        TUser result = tUserService.update(tUser);
        if (result != null) {
            return new RestResultBuilder<>().success("修改成功", result);
        }
        return new RestResultBuilder<>().error("修改失败");
    }

}
